#include <machine/asmdefs.h>

ASM_HEADER

FNDEFN_BEGIN(preamble_sync_asm)

        lr              %vr0,%r8,0          ||      lir        %r15,10          # set loop counter to 10 iters
        lr              %vr1,%r8,32         ||      ctsr       %r15,4
        lr              %vr2,%r8,64         ||      rsubf      %vr4,%vr4,%vr4   # set vr4 to zero vector
        lr              %vr3,%r8,96         ||      lir        %r15,0
        lr              %vr5,%r8,128        ||      ctsr       %r15,72          # set ac0 to zero
        lr              %vr6,%r9,0          ||      ctsr       %r15,0x45        # set mpos to zero

        /* 96 point multiply and reduce (6 rmulreds instructions) */
LOOP_PREAMBLE:
        lr              %vr7,%r9,32         ||      rshift      %vr4,%ac0,0     ||  ctsr            %r15,72
        rmulreds        %ac0,%vr0,%vr6      ||      lr          %vr6,%r9,64
        rmulreds        %ac0,%vr1,%vr7      ||      lr          %vr7,%r9,96
        rmulreds        %ac0,%vr2,%vr6      ||      lr          %vr6,%r9,128
        rmulreds        %ac0,%vr3,%vr7      ||      lr          %vr7,%r9,160
        lr              %vr2,%r8,160        ||      addil       %r9,%r9,22
        rmulreds        %ac0,%vr5,%vr6      ||      lr          %vr6,%r9,0
        rmulreds        %ac0,%vr2,%vr7      ||      lr          %vr2,%r8,64     ||  loop    0,%lc0,LOOP_PREAMBLE

        rshift          %vr4,%ac0,0         ||      ctsr        %r15,0x44       # set mcount to zero
        rmuls           %vr4,%vr4,%vr4      ||      ctsr        %r15,72         # set ac0 to zero
        rsearchmax      %ac0,%vr4
        cfsr            %r8,0x45            # get mpos, subtract 6 to get correct index (because only 10=16-6 rshifts done)
        addi            %r8,%r8,-6          ||      ji		%jt0,8

FNDEFN_END(preamble_sync_asm)

